package com.hbx.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hbx.dto.report.ReportDto;
import com.hbx.model.Notice;
import com.hbx.model.Resource;
import com.hbx.model.User;
import com.hbx.service.AsyncTaskService;
import com.hbx.service.NoticeService;
import com.hbx.service.ResourceService;
import com.hbx.service.UserService;
import com.hbx.utils.DateUtil;
import com.hbx.utils.MailSendUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.mail.MessagingException;
import java.util.Date;
import java.util.List;
import java.util.StringJoiner;
import java.util.stream.Collectors;

/*
 * @Author:hbx
 *
 * */
@Service
@Slf4j
public class AsyncTaskServiceImpl implements AsyncTaskService {

    @Autowired
    private UserService userService;

    @Autowired
    private ResourceService resourceService;

    @Autowired
    private NoticeService noticeService;

    @Autowired
    private MailSendUtil mailSendUtil;

    @Async
    @Override
    public void sendMailReport(ReportDto dto, User user) {
        Resource resource = resourceService.getById(dto.getResId());
        if (resource == null){
            log.error("资源不存在,邮件发送失败");
            return;
        }
        User resUser = userService.getById(resource.getResUserid());
        if (resUser == null){
            log.error("用户不存在,邮件发送失败");
            return;
        }
        QueryWrapper<Notice> wrapper = new QueryWrapper();
        wrapper.eq("is_delete",0);
        List<Notice> notices = noticeService.list(wrapper);
        if (CollectionUtils.isEmpty(notices)){
            log.info("【反馈模块】:暂无通知人");
            return;
        }
        List<String> strings = notices.stream().map(Notice::getNoticeEmail).collect(Collectors.toList());
        String content = generMailContent(dto, user, resUser, resource);
        try {
            String subject = "系统收到一封举报邮件,请及时处理";
            mailSendUtil.send(subject, strings.toArray(new String[0]), content);
            log.info("【邮件模块】:邮件发送成功,{}",strings);
        } catch (MessagingException e) {
            log.error("【邮件模块】:邮件发送失败,{}",e.getMessage());
        }
    }

    /**
     * 生成邮件内容
     * @param reportDto
     * @return
     */
    private String generMailContent(ReportDto reportDto, User jUser, User bUser, Resource resource) {
        StringBuilder all = new StringBuilder();
        StringJoiner joiner = new StringJoiner("<br/>");
        all.append("<!DOCTYPE html>");
        all.append("<html lang='en'>");
        all.append("<head>");
        all.append("<meta charset='UTF-8'>");
        all.append("<title>举报反馈通知</title>");
        all.append("</head>");
        all.append("<body>");
        joiner.add("举报人id:"+jUser.getUserId()+"");
        joiner.add("举报人用户名:"+jUser.getLoginName()+"");
        joiner.add("被举报人用户名:"+bUser.getLoginName()+"");
        joiner.add("举报资源id:"+resource.getResId()+"");
        joiner.add("举报原因:"+reportDto.getReportWhy()+"");
        joiner.add("举报类型:"+reportDto.getReportType()+"");
        joiner.add("举报时间:"+ DateUtil.toStringDate(new Date()) +"");
        List<String> images = reportDto.getReportImages();
        if (!CollectionUtils.isEmpty(images)){
            StringBuilder sb = new StringBuilder();
            images.stream().forEach(img -> {
                sb.append("<img width='150' style=\"margin-right='10px'\" height='100' src='");
                sb.append(img);
                sb.append("' title='图片'/>");
            });
            joiner.add("图片材料:"+sb.toString()+"");
        }
        all.append(joiner.toString());
        all.append("</body>");
        all.append("</html>");
        return all.toString();
    }
}
